package com.neusoft.training.podcast.mapper;

import java.sql.Date;
import java.sql.Timestamp;
import java.util.List;

import org.apache.ibatis.annotations.Delete;
import org.apache.ibatis.annotations.Insert;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;
import org.apache.ibatis.annotations.Update;

import com.neusoft.training.podcast.entity.Comment;

public interface CommentMapper {
	
	@Delete("delete from comment where commentID=#{commentID}")
	void cleanComemnt(@Param("commentID")Integer commentID);
	
	@Update("update comment set commentIsDel=1 where commentID=#{commentID}")
	void deleteComment(@Param("commentID")Integer commentID);
	
	@Select("select * from comment where commentIsDel = 0")
	List<Comment> findAll();
		
	@Select("select * from comment where context like CONCAT(CONCAT('%', #{context}), '%')")
	List<Comment> findCommentByContext(@Param("context") String context);
	
	@Select("select * from comment where commentID=#{commentID}")
	Comment findByID(@Param("commentID") Integer commentID);
	
	/*
	 * 2018.3.18
	 * @author Cindy*/
	@Insert("insert into comment(commentUserID,commentVideoID,context,"
			+ "commentTime,addCommentID,commentIsDel) values(#{commentUserID},"
			+ "#{commentVideoID},#{context},#{commentTime},#{addCommentID},0)")
	void insert(@Param("commentUserID") Integer commentUserID,
			@Param("commentVideoID") String commentVideoID,
			@Param("context") String context,
			@Param("commentTime") Timestamp commentTime,
			@Param("addCommentID") Integer addCommentID,
			@Param("commentIsDel") Integer commentIsDel);
	
	@Select("select * from comment where commentVideoID=#{commentVideoID} and"
			+ " addCommentID=0 and commentIsDel=0")
	List<Comment> findCommentByVideoID(@Param("commentVideoID") String commentVideoID);
	
	// 选出对某个一级评论的所有二级评论
	@Select("select * from comment where commentVideoID=#{commentVideoID} and"
			+ " addCommentID=#{commentID} and commentIsDel=0 order by commentID desc")
	List<Comment> findSecondaryComment(
			@Param("commentVideoID") String commentVideoID,
			@Param("commentID") Integer commentID);
	
	//后端逻辑添加 author：llx
		@Select("select * from comment where commentVideoID in (select videoID from video where authorID = #{userID})  && commentIsDel=0")
		List<Comment> findVideoComment(@Param("userID") Integer userID);
		
		@Select("select count(*) from comment where commentVideoID=#{videoID}") 
		Integer findCommentCount(@Param("videoID") String videoID);
		
		@Select("select * from comment where commentUserID=#{userID} && commentIsDel=0")
		List<Comment> findSelfComment(@Param("userID") Integer userID);
		
		@Select("select * from comment where commentID in (select addCommentID from comment where commentUserID=#{userID})")
		List<Comment> findOtherComment(@Param("userID") Integer userID);


}
